package com.iteaj.izone;

import com.iteaj.izone.spi.event.AsyncGuavaEventBus;
import com.iteaj.izone.spi.event.EventPublisher;
import com.iteaj.izone.spi.event.Listener;
import com.iteaj.izone.spi.event.VoidEventPublisher;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.task.TaskExecutor;

import java.util.List;

/**
 * create time: 2020/4/21
 *
 * @author iteaj
 * @since 1.0
 */
@Configuration
public class IzoneConfiguration {

    @Bean
    @Primary
    @ConditionalOnBean({TaskExecutor.class})
    @ConditionalOnMissingBean(EventPublisher.class)
    public EventPublisher eventPublisher(TaskExecutor executor, List<Listener> listeners) {
//        List<Listener> listeners = factory.getIfAvailable();
        return new AsyncGuavaEventBus(listeners, executor);
    }

    @Bean
    @ConditionalOnMissingBean(EventPublisher.class)
    public EventPublisher eventPublisher() {
        return new VoidEventPublisher();
    }

}
